package cn.com.cifi.mars.service.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import cn.com.cifi.mars.bean.dto.ProjectByStagesDto;
import cn.com.cifi.mars.constant.Constants;
import cn.com.cifi.mars.constant.HrOrgTypeEunm;
import cn.com.cifi.mars.entity.common.IdmUser;
import cn.com.cifi.mars.entity.common.SysUserRole;
import cn.com.cifi.mars.mapper.mars.IdmPositionMapper;
import cn.com.cifi.mars.mapper.mars.IdmUserMapper;
import cn.com.cifi.mars.mapper.mars.MonitorConfigDetailMapper;
import cn.com.cifi.mars.mapper.mars.MonitorWarningMapper;
import cn.com.cifi.mars.mapper.mars.MonitorWarningPortalMapper;
import cn.com.cifi.mars.mapper.mars.MonitorWarningQuotaMapper;
import cn.com.cifi.mars.mapper.mars.ProjectByStagesMapper;
import cn.com.cifi.mars.mapper.mars.SysPositionGroupAuthorizeMapper;
import cn.com.cifi.mars.mapper.mars.SysRoleMapper;
import cn.com.cifi.mars.mapper.mars.SysUserRoleMapper;
import cn.com.cifi.mars.service.CommonService;
import cn.com.cifi.mars.service.IdmUserService;
import cn.com.cifi.mars.service.MonitoringUserConsoleService;
import cn.com.cifi.mars.service.SysLogService;
import lombok.extern.slf4j.Slf4j;

/**
 * 监控台-用户权限
 * @author Li
 *
 */
@Slf4j
@Service
public class MonitoringUserConsoleServiceImpl implements MonitoringUserConsoleService{

	@Autowired
	private SysLogService sysLogService;
	@Autowired
	private ProjectByStagesMapper projectByStagesMapper;
	@Autowired
	private IdmUserMapper idmUserMapper;
	@Autowired
	private SysRoleMapper sysRoleMapper;
	@Autowired
	private SysUserRoleMapper sysUserRoleMapper;
	@Autowired
	private IdmPositionMapper idmPositionMapper;
	@Autowired
	private SysPositionGroupAuthorizeMapper sysPositionGroupAuthorizeMapper;
	@Autowired
	private IdmUserService idmUserService;

	/**
	 * 获取监控台用户的数据权限
	 */
	@Override
	public List<ProjectByStagesDto> getMonitoringUserDataPermissions(String userId) {
		//1.查询用户是否存在角色
		List<SysUserRole>roleList=sysUserRoleMapper.selectByUserId(userId);
		if(roleList.size()>0) {
			List<ProjectByStagesDto> list= getUserRoleV3(roleList);
			return list;
		}else {
			//2.查询用户是否设置岗位组 岗位权限
			List<ProjectByStagesDto> list=getUserPositionV3(userId);
			return list;
		}
	}
	
	
	/**
	 * 优化用户角色,获取角色的组织机构信息->数据权限 项目分期
	 * @param roleList
	 */
	public List<ProjectByStagesDto> getUserRoleV3(List<SysUserRole>roleList) {
		List<ProjectByStagesDto> dtoList=new ArrayList<>();
		HashSet<ProjectByStagesDto>list=new HashSet<>();
		List<String> roleIds=new ArrayList<>();
		for (SysUserRole role : roleList) {
			String roleId=role.getRoleId();
			roleIds.add(roleId);
		}
		//旭辉集团的 全部查出来
		List<String>xhIds=projectByStagesMapper.selectStagesId(roleIds, HrOrgTypeEunm.ORG_11.getKey());		
		//区域的id 全部查出来
		List<String>regionIds=projectByStagesMapper.selectStagesId(roleIds, HrOrgTypeEunm.ORG_20.getKey());
		//城市公司的id 全部查出来
		List<String>cityIds=projectByStagesMapper.selectStagesId(roleIds, HrOrgTypeEunm.ORG_21.getKey());
		//直管项目的id 全部查出来
		List<String>tubeIds=projectByStagesMapper.selectStagesId(roleIds, HrOrgTypeEunm.ORG_23.getKey());
		//项目的id 全部查出来
		List<String>projectIds=projectByStagesMapper.selectStagesId(roleIds, HrOrgTypeEunm.ORG_31.getKey());
		return getOrgList(xhIds, regionIds, cityIds, tubeIds, projectIds);
	}
	
	
	/**
	 * 优化用户岗位,岗位组,获取岗位,岗位组的组织机构信息->项目分期树
	 * 数据权限到岗位->设置自动授权后就有他的岗位组数据权限
	 * @param roleList
	 */
	public List<ProjectByStagesDto> getUserPositionV3(String userId) {
		List<ProjectByStagesDto> dtoList=new ArrayList<>();
		// 根据用户id或者用户信息
		IdmUser user=idmUserMapper.selectByUserName(userId);
		HashSet<ProjectByStagesDto>list=new HashSet<>();
		List<ProjectByStagesDto>groupList=new ArrayList<>();
		List<String>positionIds=new ArrayList<>();
		List<ProjectByStagesDto>positionList=new ArrayList<>();
		if(user!=null) {
			//查询有没有兼职岗位
			if(StringUtils.isNotEmpty(user.getParttimepost())) {
				//5.查询是否兼职岗位授权
				String [] post=user.getParttimepost().split(";");
				for (String string : post) {
					positionIds.add(string);
				}
			}
			if(StringUtils.isNotEmpty(user.getPositionid())) {
				positionIds.add(user.getPositionid());
			}
			//根据兼职岗位查询岗位组
			List<String>positionGroupIds=new ArrayList<>();
			if(positionIds.size()>0) {
				positionGroupIds=idmPositionMapper.selectPositionGroupIds(positionIds);
			}
			
			if(StringUtils.isNotEmpty(user.getIDMJobdefid())) {
				positionGroupIds.add(user.getIDMJobdefid());
			}
			
			//根据多个岗位组id查询只授权的岗位id集合
			List<String> positionIdList=sysPositionGroupAuthorizeMapper.selectByPositionGroupIds(positionGroupIds,positionIds);
			for (String posiId : positionIdList) {
				//只查授权的岗位组下的岗位id
				List<String> orgids=getGroupOrgIds(posiId);
				if(orgids.size()>0) {
					List<ProjectByStagesDto>psdList=getPositionGroupList(orgids);
					groupList.addAll(psdList);
				}
			}
			positionList=getPositionListV3(positionIds);
		}
		list.addAll(groupList);
		list.addAll(positionList);
		dtoList.addAll(list);
		return dtoList;
	}
	

	/**
	 * 优化岗位,兼职岗位->数据权限
	 * @param pbs
	 * @return
	 */
	public List<ProjectByStagesDto> getPositionListV3(List<String>positionIds) {
		List<ProjectByStagesDto> dtoList=new ArrayList<>();
		if(positionIds.size()==0) {
			return dtoList;
		}
		//旭辉集团的 全部查出来
		List<String>xhIds=projectByStagesMapper.selectStagesByPositionIds(positionIds, HrOrgTypeEunm.ORG_11.getKey());
				
		//区域的id 全部查出来
		List<String>regionIds=projectByStagesMapper.selectStagesByPositionIds(positionIds, HrOrgTypeEunm.ORG_20.getKey());
		//城市公司的id 全部查出来
		List<String>cityIds=projectByStagesMapper.selectStagesByPositionIds(positionIds, HrOrgTypeEunm.ORG_21.getKey());
		//直管项目的id 全部查出来
		List<String>tubeIds=projectByStagesMapper.selectStagesByPositionIds(positionIds, HrOrgTypeEunm.ORG_23.getKey());
		//项目的id 全部查出来
		List<String>projectIds=projectByStagesMapper.selectStagesByPositionIds(positionIds, HrOrgTypeEunm.ORG_31.getKey());
		
		return getOrgList(xhIds, regionIds, cityIds, tubeIds, projectIds);
	}
	
	/**
	 * 获取自动授权的岗位组的组织机构id
	 * @param orgnofullpath
	 * @return
	 */
	public List<String> getGroupOrgIds(String positionId) {
		List<String>orgids=new ArrayList<>();
		String belongDepFullNo=idmPositionMapper.selectBelongDepFullNoList(positionId);
		if(StringUtils.isEmpty(belongDepFullNo)) {
			return orgids;
		}
		String []path=belongDepFullNo.split("-");
		//根据岗位id获取部门全路径信息
		String belongDepFullName=idmPositionMapper.selectBelongDepFullNameList(positionId);
		if(belongDepFullName.contains("期")) {
			//不在我们本地数据库的分期 不显示出来
			String stagesId=path[belongDepFullNo.split("-").length-1];
			ProjectByStagesDto bb =projectByStagesMapper.selectByOrgdepid(stagesId);
			if(null==bb) {
				return orgids;
			}
		}

		for (int i=path.length-1;i>=0;i--) {
			String str=path[i];
			ProjectByStagesDto bb =projectByStagesMapper.selectByOrgdepid(str);
			if(bb!=null) {
				if(StringUtils.isNotEmpty(bb.getHrOrgTypeId())) {
					if(bb.getHrOrgTypeId().equals(HrOrgTypeEunm.ORG_31.getKey())) {
						orgids.add(str);
						break;
					}else if(bb.getHrOrgTypeId().equals(HrOrgTypeEunm.ORG_23.getKey())) {
						orgids.add(str);
						break;
					}else if(bb.getHrOrgTypeId().equals(HrOrgTypeEunm.ORG_21.getKey())) {
						orgids.add(str);
						break;
					}else if(bb.getHrOrgTypeId().equals(HrOrgTypeEunm.ORG_20.getKey())) {
						orgids.add(str);
						break;
					}else if(bb.getHrOrgTypeId().equals(HrOrgTypeEunm.ORG_11.getKey())) {
						orgids.add(str);
						break;
					}
				}
			}
		}
		return orgids;
	}
	
	
	/**
	 * 优化岗位组->数据权限-》自动授权的组织机构权限
	 * @param pbs
	 * @return
	 */
	public List<ProjectByStagesDto> getPositionGroupList(List<String>positionIds) {
		List<ProjectByStagesDto> dtoList=new ArrayList<>();
		if(positionIds.size()==0) {
			return dtoList;
		}
		//旭辉集团的 全部查出来
		List<String>xhIds=projectByStagesMapper.selectStagesByPositionGroupIds(positionIds, HrOrgTypeEunm.ORG_11.getKey());
				
		//区域的id 全部查出来
		List<String>regionIds=projectByStagesMapper.selectStagesByPositionGroupIds(positionIds, HrOrgTypeEunm.ORG_20.getKey());
		//城市公司的id 全部查出来
		List<String>cityIds=projectByStagesMapper.selectStagesByPositionGroupIds(positionIds, HrOrgTypeEunm.ORG_21.getKey());
		//直管项目的id 全部查出来
		List<String>tubeIds=projectByStagesMapper.selectStagesByPositionGroupIds(positionIds, HrOrgTypeEunm.ORG_23.getKey());
		//项目的id 全部查出来
		List<String>projectIds=projectByStagesMapper.selectStagesByPositionGroupIds(positionIds, HrOrgTypeEunm.ORG_31.getKey());
		
		return getOrgList(xhIds, regionIds, cityIds, tubeIds, projectIds);
	}
	
	/**
	 * 优化获取组织机构信息
	 * @param xhIds
	 * @param regionIds
	 * @param cityIds
	 * @param tubeIds
	 * @param projectIds
	 * @return
	 */
	public List<ProjectByStagesDto> getOrgList(List<String>xhIds,List<String>regionIds,List<String>cityIds,List<String>tubeIds,
			List<String>projectIds) {
		List<ProjectByStagesDto> dtoList=new ArrayList<>();
		HashSet<ProjectByStagesDto>list=new HashSet<>();
		if(xhIds.size()>0) {
			List<ProjectByStagesDto>xhList=projectByStagesMapper.selectOrgNoStagesList(xhIds,HrOrgTypeEunm.ORG_32.getKey());
			for (ProjectByStagesDto p : xhList) {
				//可以点击
				p.setSelectable(Constants.TRUE);
			}
			list.addAll(xhList);
		}
		if(regionIds.size()>0) {
			List<ProjectByStagesDto>regionList=projectByStagesMapper.selectOrgNoStagesList(regionIds,HrOrgTypeEunm.ORG_32.getKey());
			for (ProjectByStagesDto p : regionList) {
				//可以点击
				p.setSelectable(Constants.TRUE);
			}
			list.addAll(regionList);
		}
		if(cityIds.size()>0) {
			//根据城市id获取城市和城市以下所有数据
			List<ProjectByStagesDto>cityList=projectByStagesMapper.selectOrgNoStagesList(cityIds,HrOrgTypeEunm.ORG_32.getKey());
			for (ProjectByStagesDto p : cityList) {
				//可以点击
				p.setSelectable(Constants.TRUE);
			}
			//根据城市id获取区域信息
			List<ProjectByStagesDto>regionList=projectByStagesMapper.selectOrgParentList(cityIds);
			for (ProjectByStagesDto p : regionList) {
				p.setSelectable(Constants.FALSE);
			}
			list.addAll(cityList);
			list.addAll(regionList);
		}
		if(tubeIds.size()>0) {
			//根据直管项目id获取直管项目和以下所有数据
			List<ProjectByStagesDto>tubeList=projectByStagesMapper.selectOrgNoStagesList(tubeIds,HrOrgTypeEunm.ORG_32.getKey());
			for (ProjectByStagesDto p : tubeList) {
				//可以点击
				p.setSelectable(Constants.TRUE);
			}
			//根据直管项目id获取区域信息
			List<ProjectByStagesDto>regionList=projectByStagesMapper.selectOrgParentList(tubeIds);
			for (ProjectByStagesDto p : regionList) {
				p.setSelectable(Constants.FALSE);
			}
			list.addAll(tubeList);
			list.addAll(regionList);
		}
		if(projectIds.size()>0) {
			//根据项目id获取项目和以下所有数据
			List<ProjectByStagesDto>projectList=projectByStagesMapper.selectOrgNoStagesList(projectIds,HrOrgTypeEunm.ORG_32.getKey());
			for (ProjectByStagesDto p : projectList) {
				//可以点击
				p.setSelectable(Constants.TRUE);
			}
			//根据项目id获取城市公司信息
			List<ProjectByStagesDto>cityList=projectByStagesMapper.selectOrgParentList(projectIds);
			List<String>cityStr=new ArrayList<>();
			for (ProjectByStagesDto c : cityList) {
				cityStr.add(c.getOrgdepid());
				c.setSelectable(Constants.FALSE);
			}
			//根据城市公司id获取区域信息
			List<ProjectByStagesDto>regionList=projectByStagesMapper.selectOrgParentList(cityStr);
			for (ProjectByStagesDto p : regionList) {
				p.setSelectable(Constants.FALSE);
			}
			list.addAll(projectList);
			list.addAll(cityList);
			list.addAll(regionList);
		}
		dtoList.addAll(list);
		return dtoList;
	}

}
